#include "main_window.h"

MainWindow::MainWindow()
{
  this->createActions();
  this->createMenus();
  statusBar()->showMessage(tr("Ready"));
  this->gameParams = new GameParams;
  connect(&newGameWizard, SIGNAL(makeNewGameSig())
	  , this, SLOT(makeNewGameSlo()));
  this->oneGameStarted = false;

}

MainWindow::~MainWindow()
{
  if(this->oneGameStarted == true)
    {
      //delete(this->dockCommands);
    }
  //delete(this->gameParams);
  
    
}

void		MainWindow::createActions()
{
  // create game menu actions
  gameNewGameAct = new QAction(QIcon("../img/cell.png"), tr("&New game"), this);
  gameNewGameAct->setShortcut(tr("Ctrl+N"));
  gameNewGameAct->setStatusTip(tr("Start a new game"));
  connect(gameNewGameAct, SIGNAL(triggered()), this, SLOT(makeNewGameWizard()));

  gameOpenAct = new QAction(QIcon("../img/restore.png"), tr("&Open a game"), this);
  gameOpenAct->setShortcut(tr("Ctrl+O"));
  gameOpenAct->setStatusTip(tr("Open a previously saved game from hard disk"));

  gameSaveAct = new QAction(QIcon("../img/disk.png"), tr("&Save current game"), this);
  gameSaveAct->setShortcut(tr("Ctrl+S"));
  gameSaveAct->setStatusTip(tr("Save current game to disk"));

  gameExitAct = new QAction(QIcon("../img/exit.png"), tr("E&xit"), this);
  gameExitAct->setShortcut(tr("Ctrl+Q"));
  gameExitAct->setStatusTip(tr("Exit the application"));
  connect(gameExitAct, SIGNAL(triggered()), this, SLOT(close()));

  // create view menu actions
  viewGalaxyAct = new QAction(QIcon("../img/galaxy.png"), tr("&Galaxy"), this);
  viewGalaxyAct->setStatusTip("Display the galaxy tab");
  viewGalaxyAct->setCheckable(true);

  viewSystemAct = new QAction(QIcon("../img/system.png"), tr("&System"), this);
  viewSystemAct->setStatusTip("Display the system tab");
  viewSystemAct->setCheckable(true);

  viewPlanetAct = new QAction(QIcon("../img/planet.png"),tr("&Planet"), this);
  viewPlanetAct->setStatusTip("Display the planet tab");
  viewPlanetAct->setCheckable(true);

  viewScienceAct = new QAction(QIcon("../img/science.png"),tr("S&cience"), this);
  viewScienceAct->setStatusTip("Display the science tab");
  viewScienceAct->setCheckable(true);

  viewDiplomacyAct = new QAction(QIcon("../img/diplomacy.png"),tr("&Diplomacy"), this);
  viewDiplomacyAct->setStatusTip("Display the diplomacy tab");
  viewDiplomacyAct->setCheckable(true);

}

void		MainWindow::createMenus()
{
  // create game menu
  gameMenu = menuBar()->addMenu(tr("&Game"));
  gameMenu->addAction(gameNewGameAct);
  gameMenu->addAction(gameOpenAct);
  gameMenu->addAction(gameSaveAct);
  gameMenu->addSeparator();
  gameMenu->addAction(gameExitAct);

  // create view menu
  viewMenu = menuBar()->addMenu(tr("&View"));
  viewMenu->addAction(viewGalaxyAct);
  viewMenu->addAction(viewSystemAct);
  viewMenu->addAction(viewPlanetAct);
  viewMenu->addAction(viewScienceAct);
  viewMenu->addAction(viewDiplomacyAct);

  // create optionMenu
  optionMenu = menuBar()->addMenu(tr("&Options"));
}

void		MainWindow::makeNewGameWizard()
{
  //newGameWizard = new NewGameWizard(this);
  //newGameWizard->show();


  newGameWizard.restart();
  newGameWizard.setGameParams(this->gameParams);
  newGameWizard.show();


}

void		MainWindow::makeNewGameSlo()
{
  std::cout << "gameConfigChanged !!!" << std::endl;
  std::cout << "Galxy size :" << this->gameParams->getStarDensity() << std::endl;

  // set at least one game have been started so we need to alloc memory
  // for it

  this->gameCore.generate(this->gameParams);
  this->createGameToolBar();
  this->createTabSet();
  this->createGameDocks();
}

void		MainWindow::createTabSet()
{
  this->tabSet.setGameCorePtr(&this->gameCore);
  this->setCentralWidget(&tabSet);
}

void		MainWindow::createGameDocks()
{
  if(this->oneGameStarted == false)
    {
      this->oneGameStarted = true;
      this->addDockWidget(Qt::LeftDockWidgetArea, &this->dockCommands);
      
      // signals connection
      connect(&this->dockCommands.tabs
	      , SIGNAL(galaxyZoomOut())
	      , &this->tabSet
	      , SLOT(galaxyZoomOut()));
      connect(&this->dockCommands.tabs
	      , SIGNAL(galaxyZoomIn())
	      , &this->tabSet
	      , SLOT(galaxyZoomIn()));
      connect(&this->dockCommands.tabs
	      , SIGNAL(galaxyRotateUp())
	      , &this->tabSet
	      , SLOT(galaxyRotateUp()));
      connect(&this->dockCommands.tabs
	      , SIGNAL(galaxyRotateDown())
	      , &this->tabSet
	      , SLOT(galaxyRotateDown()));
      connect(&this->dockCommands.tabs
	      , SIGNAL(galaxyRotateLeft())
	      , &this->tabSet
	      , SLOT(galaxyRotateLeft()));
      connect(&this->dockCommands.tabs
	      , SIGNAL(galaxyRotateRight())
	      , &this->tabSet
	      , SLOT(galaxyRotateRight()));
      connect(&this->dockCommands.tabs
	      , SIGNAL(galaxyResetView())
	      , &this->tabSet
	      , SLOT(galaxyResetView()));
      connect(&this->dockCommands.tabs
	      , SIGNAL(showLinks(int))
	      , &this->tabSet
	      , SLOT(galaxyShowLinks(int)));
    }
  
}

void		MainWindow::createGameToolBar()
{
  this->addToolBar(&this->gameToolBar);
}
